package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.LabelprintFormMap;
import com.iemes.entity.ReprintenterFormMap;
import com.iemes.entity.ReprintlabelFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.ReprintenterMapper;
import com.iemes.mapper.warehouse.ReprintlabelMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/label_reprint/")
@Controller
public class Label_ReprintController extends BaseController {

	@Inject
	private ReprintenterMapper reprintenterMapper;
	
	@Inject 
	private ReprintlabelMapper reprintlabelMapper;

	// 查看补打标签表，显示按钮。
	@RequestMapping("label_reprint")
	public String label_reprint(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/label_reprint_list";
	}

	// 补打标签表分页
	@ResponseBody
	@RequestMapping("label_reprint_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		ReprintenterFormMap reprintenterFormMap = getFormMap(ReprintenterFormMap.class);
		List<Map<String, Object>> reprint=reprintenterMapper.getAll(reprintenterFormMap);
		LuogicPaging(pageNow, pageSize, reprint);
		pageView.setRecords(reprint);
		return pageView;
	}

	// 新增补打标签表页面
	@RequestMapping("label_reprint_add")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/warehouse/label_reprint_add";
	}

	// 新增补打标签表操作
	@ResponseBody
	@RequestMapping(value = "label_reprint_addEntity")
	@SystemLog(module = "编辑补打标签", methods = "打印标签-新增补打标签") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request) throws Exception {
		ReprintenterFormMap reprintenterFormMap=getFormMap(ReprintenterFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		reprintenterFormMap.put("date", sdf.format(date));
		reprintenterFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		reprintenterFormMap.put("generatedLabel", 0);
		baseMapper.addEntity(reprintenterFormMap);
		return "success";
	}
	
	// 编辑补打标签表
	@RequestMapping("label_reprint_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据Id获得入库报表
			Map<String, Object> enter=reprintenterMapper.getById(id);
			if(Integer.parseInt(enter.get("scheduleId").toString())==1){
				enter.put("scheduleId", "甲班");
			}else{
				enter.put("scheduleId", "乙班");
			}
			if(Integer.parseInt(enter.get("receiptType").toString())==1){
				enter.put("receiptType", "补打条码");
			}
			model.addAttribute("enter",enter);
			//获得相同reprintenterId的补打标签表
			ReprintlabelFormMap reprintlabelFormMap=getFormMap(ReprintlabelFormMap.class);
			reprintlabelFormMap.put("reprintenterId", enter.get("id"));
			List<ReprintlabelFormMap> label=reprintlabelMapper.getReprintLabel(reprintlabelFormMap);
			model.addAttribute("label",label);
		}
		return Common.BACKGROUND_PATH + "/warehouse/label_reprint_edit";
	}

	// 修改补打标签表
	@ResponseBody
	@RequestMapping(value = "label_reprint_editEntity")
	@SystemLog(module = "编辑补打标签", methods = "打印标签-修改补打标签") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		ReprintenterFormMap reprintenterFormMap = getFormMap(ReprintenterFormMap.class);
		//先删除
		baseMapper.deleteByAttribute("reprintenterId","'"+Integer.parseInt(reprintenterFormMap.getStr("id"))+"'", ReprintlabelFormMap.class);
		// 新增标签子项
		int reprintenterId=Integer.parseInt(request.getParameter("reprintenterFormMap.id"));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		//计算“已生产产品标签数”
		int total=0;
		String[] produceVolumn = request.getParameterValues("produceVolumn_1");
		String[] perCount = request.getParameterValues("perCount_1");
		if(produceVolumn!=null){
			for(int i = 0 ;i<produceVolumn.length;i++){
				ReprintlabelFormMap reprintlabelFormMap=new ReprintlabelFormMap();
				reprintlabelFormMap.put("reprintenterId",reprintenterId);
				reprintlabelFormMap.put("produceVolumn", produceVolumn[i]);
				reprintlabelFormMap.put("perCount", perCount[i]);
				if(!produceVolumn[i].isEmpty()&&!perCount[i].isEmpty()){
					total+=Integer.parseInt(produceVolumn[i])/Integer.parseInt(perCount[i]);
				}
				reprintlabelFormMap.put("date", sdf.format(date));
				reprintlabelFormMap.put("fillPeopleId",userFormMap.getInt("id"));
				baseMapper.addEntity(reprintlabelFormMap);
				reprintenterFormMap.put("count", total);
				reprintenterFormMap.put("generatedLabel", 1);
				baseMapper.editEntity(reprintenterFormMap);
			}
		}else{
			reprintenterFormMap.put("count", 0);
			reprintenterFormMap.put("generatedLabel", 0);
			baseMapper.editEntity(reprintenterFormMap);
		}
		return "success";
	}
}


